﻿using System;

class BaseSToBaseD
{
    static void Main()
    {
        string strNum = "3FF";
        int baseS = 16; // This base must correspond to the number (strNum)
        int baseD = 2;

        Console.WriteLine("Base {0}: {1}", baseS, strNum);
        Console.WriteLine("Base {0}: {1}", baseD, ConvertBaseSToBaseD(strNum, baseS, baseD));
    }

    private static string ConvertBaseSToBaseD(string strNum, int baseS, int baseD)
    {
        // Converting S number to Decimal number
        int decimalS = ConvertBaseSToDecimal(strNum, baseS);

        //Converting Decimal number to D number
        string baseDNum = ConvertDecimalToBaseD(decimalS, baseD);

        return baseDNum;
    }

    private static int ConvertBaseSToDecimal(string strNum, int baseS)
    {
        int decimalS = 0;
        int len = strNum.Length - 1;

        for (int i = 0; i <= len; i++)
        {
            // Conversion: char -> string -> int
            int digit = ConvertToDecimalDigit(strNum[i].ToString());
            decimalS += digit * (int)Math.Pow(baseS, len - i);
        }

        return decimalS;
    }

    private static int ConvertToDecimalDigit(string baseSDigit)
    {
        int deciDigit = 0;

        switch (baseSDigit)
        {
            case "A":
                deciDigit = 10;
                break;
            case "B":
                deciDigit = 11;
                break;
            case "C":
                deciDigit = 12;
                break;
            case "D":
                deciDigit = 13;
                break;
            case "E":
                deciDigit = 14;
                break;
            case "F":
                deciDigit = 15;
                break;
            default:
                deciDigit = int.Parse(baseSDigit);
                break;
        }

        return deciDigit;
    }

    private static string ConvertDecimalToBaseD(int deciNum, int baseD)
    {
        string baseDNum = String.Empty;
        int remainder = 0;

        do
        {
            remainder = deciNum % baseD;
            baseDNum = ConvertToBaseDDigit(remainder) + baseDNum;
            deciNum = deciNum / baseD;
        }
        while (deciNum != 0);

        return baseDNum;
    }

    private static string ConvertToBaseDDigit(int deciDigit)
    {
        string baseDDigit = String.Empty;

        switch (deciDigit)
        {
            case 10:
                baseDDigit = "A";
                break;
            case 11:
                baseDDigit = "B";
                break;
            case 12:
                baseDDigit = "C";
                break;
            case 13:
                baseDDigit = "D";
                break;
            case 14:
                baseDDigit = "E";
                break;
            case 15:
                baseDDigit = "F";
                break;
            default:
                baseDDigit = deciDigit.ToString();
                break;
        }

        return baseDDigit;
    }
}
